﻿using System;
using System.Windows;
using appDataContracts;
using appInterfaces;
using System.Windows.Media.Imaging;

namespace appSupplier
{
    /// <summary>
    /// Logique d'interaction pour LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        ISupplier supplierService = null;
        mySupplierService.SupplierClient client = null;

        public LoginWindow()
        {

            InitializeComponent();
            tbLogin.Text = "Apple";
            tbPassword.Password = "applecagere";
            tbLogin.Focus();
        }

        private void btQuit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                client = new mySupplierService.SupplierClient();

                //supplierService = new ChannelFactory<ISupplier>("clientHttp").CreateChannel();

                //SerializedSupplier supplier = supplierService.supplierLogin(tbLogin.Text, tbPassword.Password);

                SerializedSupplier supplier = client.supplierLogin(tbLogin.Text, tbPassword.Password);

                if (supplier != null)
                {
                    if (supplier.SUPPLIERDATELIMIT.CompareTo(DateTime.Now) > 0)
                    {
                        this.Visibility = Visibility.Hidden;
                        new AppWindow(supplier, client).Show();
                        //new AppWindow(supplier, this.supplierService).Show();
                    }
                    else
                    {
                        lblError.Content = "Compte expiré";
                    }
                }
                else
                {
                    lblError.Content = "Mauvaises informations de login.";
                }
            }
            catch (Exception ex)
            {
                lblError.Content = "Erreur de connexion au serveur";
            }
        }
    }
}
